<template>
	<view style="padding-top:var(--status-bar-height);">
		<!-- 头部行 -->
		<view class="tab">
			<view class="tab_line">
				<text class="line_text">积分</text>
				<image v-if="showPoint" class="icon-setting" :src="'/static/user/visible_n.png'"  @click="checkPoint"></image>
				<image v-else class="icon-setting" :src="'/static/user/nosee-green.png'"  @click="checkPoint"></image>
			</view>
			<view class="tab_line tab_line_num">
				<text class="line_num">{{ showPoint?point:'***' }}</text>	
				<!-- <image class="icon-setting" :src="'/static/user/visible.png'" ></image> -->
				<image class="line_icon" src="/static/icon/pures_tb_n.png"></image>
			</view>
			<view class="tab_line">
				<text class="line_text">当前估值（元）</text>
				<text class="line_text">估值= 当前价格 x 积分数</text>
			</view>
			<view class="tab_line tab_line_num2">
				<view class="line_num_left">
					<text class="line_num2">￥</text><text class="line_num3">{{ showPoint?totalValue:'***' }}</text>
				</view>
				<view class="line_num2">1 LHT = {{ pointValue }} 元</view>
			</view>
		</view>
		
		<!-- 转出数量行 -->
		<view class="tbnum">
			<view class="tab_line">
				<text class="tbnum_text">转出数量</text><text>注：需扣除0元手续费</text>
			</view>
			<view class="tab_line tbnum_line">
				<input class="input_class" type="number" placeholder="请输入转出数量" placeholder-class="input_placeholder" :value="outCount" @input="inputOut" />
				<text class="tbnum_line_text" @click="outCountAll">全部</text>
			</view>
		</view>
		
		<!-- 地址模块 -->
		<view class="address_item">
			<text class="address_item_title">转出LHT地址</text>
			<view class="address_item_tab">
				<input class="address_input" placeholder="地址别名" placeholder-class="address_placeholder" :value="addr.name||null" @input="inputName" />
			</view>
			<view class="address_item_tab">
				<view class="address_item_tab2">
					<input class="address_input address_input2" placeholder="输入或粘入地址" placeholder-class="address_placeholder" :value="addr.address||null" @input="inputAddress"/>
					<view class="address_btn" @click="navTo('/pages/purse/lhtAddress/lhtAddress/lhtAddress')">选择</view>
				</view>
			</view>
			<view class="address_item_tab">
				<input class="address_input" placeholder="转出说明（选填）" placeholder-class="address_placeholder" :value="remark||null" @input="inputRemark"  />
			</view>
		</view>
		<view class="address_remark">为保障资金安全，当您账户安全策略变更、密码修改、我们会对转出进行人工审核，请耐心等待工作人员联系。请务必确认电脑及浏览器安全，防止信息被篡改或泄露。</view>
		<view class="botton_btn" @click="submit">确 定</view>
		
	</view>
</template>

<script>
		
	let that;
	export default {
		data() {
			return {
				// 积分数
				point:0,
				// 当前积分价格
				pointValue:1,
				// 昨日价格
				lastDay:1,
				// 较昨日涨幅
				range:0.0,
				// 当前估值
				totalValue:0,
				// 用户详情
				userDetail:null,
				outCount:null,
				//选择的地址对象
				addr:{},
				//备注
				remark:null,
				// 是否显示
				showPoint:true,
			}
		},
		onLoad(options) {
			that = this;
		},
		methods: {
			//设置地址
			setAddress(addr){
				that.addr = addr;
			},
			// 全部积分
			outCountAll(){
				that.outCount = that.point;
			},
			// 转出输入
			inputOut(e){
				let count = e.detail.value;
				that.outCount = count;
			},
			// 输入别名
			inputName(e){
				that.addr.name = e.detail.value;
			},
			// 输入地址
			inputAddress(e){
				that.addr.address = e.detail.value;
			},
			// 输入备注
			inputRemark(e){
				that.remark= e.detail.value;
			},
			
			//  初始化数据
			initData(){
				that.outCount = null;
				that.addr = {};
				that.remark = null;
			},
			// 切换显示积分
			checkPoint(){
				that.showPoint = !that.showPoint;
			},
			
			// 确定
			submit(){
				let { point,outCount,addr,remark } = that;
				if(!outCount){
					that.$util.msg("转出数量不能为空");
					return;
				}
				if(Number(outCount)>Number(point)){
					that.$util.msg("转出数量超出积分总数");
					return;
				}
				if(!addr||!addr.address){
					that.$util.msg("转出地址不能为空");
					return;
				}
				console.log(point,outCount,addr,remark);
				// 调用转出
				that.transferPoint(outCount,addr.address,remark);
				
			},
			
			// 查询当前币的价格
			async queryPointValue(){
				await that.$request('point', 'value',{
				}).then(res=>{
					// that.$util.msg(response.msg);
					if(res){
						this.pointValue = Number(res.data||0).toFixed(2);
						that.totalValue = (that.point * that.pointValue).toFixed(2);		//计算当前估值
						that.range = ((that.pointValue-that.lastDay)/that.lastDay).toFixed(2);	//计算涨幅
						
					}
					
				}).catch(err=>{
					console.error(err);
				})
			},
			// 查询用户积分数
			async queryPointCount(){
				await that.$request('point', 'count',{
				}).then(res=>{
					console.log("查询用户积分数",res);
					if(res){
						this.point = Number(res.data||0).toFixed(4);
						that.totalValue = (that.point * that.pointValue).toFixed(2);		//计算当前估值
					}
				}).catch(err=>{
					console.error("查询用户积分失败",err);
				})
			},
			
			// 转出（自己给指定的地址转账）
			async transferPoint(point,address,remark){
				uni.showLoading({title: '提交中...'});
				await this.$request('wallet', 'transfer',{
					point,
					toAddress:address,
					remark,
					price:((point/100)*that.pointValue).toFixed(2),			
				}).then(res=>{
					uni.hideLoading();
					console.log(res);
					this.loading = false;
					// 查询用户积分数
					that.queryPointCount();
					that.initData();
					uni.showModal({
					    title: '成功',
					    content: res.msg||"转出成功",
						confirmColor:"#55BB8A",
						showCancel:false,
					    success: function (res) {
					       
					    }
					});
					
				}).catch(err=>{
					uni.hideLoading();
					this.$util.msg(err||"转出失败");
					this.loading = false;
					console.error(err);
				})
			},
		},
		// 监听页面加载
		onLoad(event){
			that = this;
		},
		// 监听页面显示
		onShow() {
			uni.showLoading({title: '加载中'});
			// 查询当前币价格
			that.queryPointValue();
			// 查询用户积分数
			that.queryPointCount();
			
			setTimeout(function () {
				uni.hideLoading();
			}, 1500);
		},
		
		// 监听页面隐藏
		onHide(){
			
		},
		// 监听页面卸载
		onUnload(){
			
		},
		// 监听页面下拉刷新
		onPullDownRefresh(){
			
		},
		// 组件：在实例创建完成后被立即调用
		created() {
			
		},
	}
</script>

<style>
	@import url("./cash.css");
</style>
